package com.tzy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tzy.common.entity.User;
import com.tzy.common.utils.EncryptUtil;
import com.tzy.common.utils.RandomUtil;
import com.tzy.user.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谭湛宇
 * @since 2022-01-04
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {
    private Logger log = LoggerFactory.getLogger("userService");
    @Resource
    private UserMapper userMapper;



    @Override
    public User login(String username, String password) {
        //加密后的密码
        String pass = EncryptUtil.getMd5(password);
        if(StringUtils.hasText(pass)){
            //条件构造器
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            //Lambda表达式
            queryWrapper.lambda().eq(User::getEmail,username).eq(User::getPassword,pass);
            User user = getOne(queryWrapper);
            if(user != null){
                setToken(user);
                //保存信息到redis里
                return user;
            }
        }
        return null;
    }

    @Override
    public User getInfo(int id) {
        return null;
    }

    private void setToken(User user){
        if(user != null){
            user.setToken(RandomUtil.genToken());
        }
    }
//    /**
//     *
//     * @param id
//     * @return
//     */
////    @Cacheable(key = "#id",unless = "#result == null")
//    public User getUserById(int id){
//        log.info("执行查询");
//        User user = userMapper.selectById(id);
//        user.setPassword(null);
//        return user;
//    }

//    @CacheEvict( key = "#user.id")
    @Transactional
    public boolean updateById(User user){
        int res = userMapper.updateById(user);
        if(res>0){
            //修改成功后
            return true;
        }
        return false;
    }
    @Transactional
    public int insert(User user){
        //给密码加密
        user.setPassword(EncryptUtil.getMd5(user.getPassword()));
        return userMapper.insert(user);
    }

    /**
     * 验证输入的邮箱是否已经注册
     */
    public User checkMail(String email){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("email",email);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 验证输入的邀请码是否存在
     */
    public User checkInvitationCode(String invitationCode){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("invitation_code",invitationCode);
        return userMapper.selectOne(wrapper);
    }
    /**
     * 验证输入的邀请码是否存在
     */
    public User checkInviteLink(String InviteLink){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("Invite_link",InviteLink);
        return userMapper.selectOne(wrapper);
    }


    public int delete(int id){
        return userMapper.deleteById(id);
    }

    public List<User> getAllUsers(){
        return userMapper.selectList(null);
    }

    /**
     * 根据用户编号查询用户信息
     * @param id 用户编号
     * @return user用户实体类
     */
    public User getUserInfoById(int id){
        User user = userMapper.selectById(id);
        user.setPassword(null);
        return user;
    }

    /**
     * 模糊查询
     */
    public List<User> getAllUsersByMoHuChaXun(String column){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("username",column);

        return userMapper.selectList(queryWrapper);
    }


}
